<template>
  <d2-container>
    <template slot="header">
      <el-form :gutter="24" ref="search-box" :model="mainQuery" label-width="auto" label-position="left" :inline="true">

        <el-form-item :label="$t('page.form.accountType')" size="mini">
          <el-select v-model="mainQuery.accountType" :placeholder="$t('page.form.all')" clearable>
            <el-option v-for="item in accountTypeOptions" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item :label="$t('page.form.timetotype')" size="mini">
          <el-select v-model="mainQuery.type" :placeholder="$t('page.form.all')" clearable>
            <el-option v-for="item in typeOptions" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item :label="$t('page.form.timestep')" size="mini">
          <el-select v-model="mainQuery.handleStatus" :placeholder="$t('page.form.all')" clearable>
            <el-option v-for="item in stepOptions" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>

        <!-- <el-form-item :label="$t('page.form.state')" size="mini">
          <el-select v-model="mainQuery.status" :placeholder="$t('page.form.all')" clearable>
            <el-option
              v-for="item in statusOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item> -->

        <el-form-item :label="$t('page.form.temporalInterval')" size="mini">
          <el-date-picker size="mini" v-model="dateTimeRange" type="datetimerange" value-format="yyyy-MM-dd HH:mm:ss" :picker-options="datetimePickerOptions" @change="onDateTimePickerChange" :start-placeholder="$t('page.form.startDate')" :end-placeholder="$t('page.form.endTime')">
          </el-date-picker>
        </el-form-item>
      </el-form>

      <div class="search-btn-box">
        <div class="left">
          <el-button-group>
            <el-tooltip :disabled="!!mainTable.current" :content="$t('page.form.selectRowData')">
              <el-button class="filter-item" icon="" type="" @click="handleDetail" :disabled="mainTable.loading" size="mini">{{$t('page.form.viewDetails')}}</el-button>
            </el-tooltip>
            <!-- <el-button class="filter-item" icon="" :type="canMoney() ? 'success' : 'info'" @click="handleAction('money')" :disabled="!canMoney()" size="mini" v-if="$options.filters['checkButtonAccess']('operatetrade-actionmoney')">{{ $t('page.form.paragraph') }}</el-button> -->
            <!-- <el-button class="filter-item" icon="" :type="canUnfreeze() ? 'success' : 'info'" @click="handleAction('unfreeze')" :disabled="!canUnfreeze()" size="mini" v-if="$options.filters['checkButtonAccess']('operatetrade-actionunfreeze')">解冻</el-button> -->

          </el-button-group>
        </div>
        <div class="right">
          <el-button class="filter-item" icon="el-icon-refresh" type="default" @click="resetMainData" :loading="mainTable.loading" size="mini">{{ $t('page.search-box.reset') }}</el-button>
          <el-button class="filter-item" icon="el-icon-search" type="primary" @click="handleSearch" :loading="mainTable.loading" size="mini">{{ $t('page.search-box.search') }}</el-button>
        </div>
      </div>
    </template>

    <!--主列表数据-->
    <d2-crud ref="d2Crud" :options="mainTable.options" :index-row="mainTable.indexRow" :columns="mainTable.columns" :data="mainTable.list" :selection-row="mainTable.allowSelect" :loading="mainTable.loading" @current-change="onMainCurrentChange" @row-dblclick="onMainDoubleClick" @selection-change=""></d2-crud>

    <template slot="footer">
      <el-pagination @size-change="handleMainSizeChange" @current-change="handleMainPageChange" :current-page="mainTable.pagination.currentPage" :page-sizes="mainTable.pagination.pageSizes" :page-size="mainTable.pagination.pageSize" :page-count="mainTable.pagination.pageCount" :total="mainTable.pagination.total" layout="total, sizes, prev, pager, next, jumper" :background="true">
      </el-pagination>
    </template>

    <dialog-detail ref="dialog-detail" @dialog-close="fetchMainData"></dialog-detail>

  </d2-container>
</template>

<script>

import util from '@/libs/util';
import DialogDetail from './dialog-detail';
import ApiMain from '@/api/trade.account';
import DatetimePickerMixin from '@/mixins/DatetimePickerMixin'
import i18nTable from "@/mixins/i18n-table";

export default {
  name: 'settle-operatetrade',
  mixins: [
    DatetimePickerMixin, i18nTable
  ],
  components: {
    'dialog-detail': DialogDetail,
  },
  data() {
    return {

      typeOptions: [],
      stepOptions: [],
      accountTypeOptions: [],
      statusOptions: [],

      mainQueryRaw: null,
      mainQuery: {
        mchNo: '',
        accountType: '',
        type: '',
        handleStatus: '',
        status: '',
      },

      mainTable: {
        loading: false,
        options: {
          border: true,
          stripe: true,
          highlightCurrentRow: true,
          emptyText: this.$t('page-announcement.d2-crud.empty-data'),
          size: 'mini',
        },
        // indexRow: {
        //   title:this.$t('page.form.serialnumber'),
        //   width: "80",
        //   align:'center'
        // },
        indexRow: null,
        allowSelect: false,
        columns: [
          { title: "商户号", t: "page-mch-ter.d2-crud.column-mchNo", key: 'mchNo', align: 'center', showOverflowTooltip: true, minWidth: 200 },
          { title: "账户类型", t: "page.form.accountType", key: 'strAccountType', align: 'center', showOverflowTooltip: true, minWidth: 200 },
          { title: "结算币种", t: "page.form.settlementCurrency", key: 'currency', align: 'left', showOverflowTooltip: true, minWidth: 150 },
          { title: "事件订单号", t: "page.form.eventOrderNumber", key: 'eventOrderNo', align: 'center', showOverflowTooltip: true, minWidth: 200 },

          {
            title: "事件变动金额", t: "page.form.amounteventchange", key: 'floatAmount', align: 'right', showOverflowTooltip: true, minWidth: 150, formatter(row, col, value, index) {
              return value.toFixed(2);
            }
          },
          { title: "事件类型", t: "page.form.eventtype", key: 'strType', align: 'center', showOverflowTooltip: true },
          { title: "事件步骤", t: "page.form.eventsteps", key: 'strHandleStatus', align: 'center', showOverflowTooltip: true, minWidth: 150 },

          {
            title: "申请", t: "page.form.apply", align: 'center', children: [
              { title: "申请备注", t: "page.form.applicationNote", key: 'createRemark', align: 'center', showOverflowTooltip: true, minWidth: 200 },
              { title: "申请时间", t: "page.form.timeApplication", key: 'createTime', align: 'center', showOverflowTooltip: true, minWidth: 160 },
              { title: "申请人", t: "page.form.proposer", key: 'createBy', align: 'center', showOverflowTooltip: true, minWidth: 120 },
            ]
          },
          {
            title: "初审意见", t: "page.form.firstTrial", align: 'center', children: [
              { title: "初审意见", t: "page.form.firstTrial", key: 'reviewRemark', align: 'center', showOverflowTooltip: true, minWidth: 200 },
              { title: "初审时间", t: "page.form.Thefirsttime", key: 'reviewTime', align: 'center', showOverflowTooltip: true, minWidth: 160 },
              { title: "初审人", t: "page.form.firstperson", key: 'reviewBy', align: 'center', showOverflowTooltip: true, minWidth: 120 },
            ]
          },
          {
            title: "复核", t: "page.form.re-check", align: 'center', children: [
              { title: "复核意见", t: "page.form.reviewOpinion", key: 'reReviewRemark', align: 'center', showOverflowTooltip: true, minWidth: 200 },
              { title: "复核时间", t: "page.form.checkthetime", key: 'reReviewTime', align: 'center', showOverflowTooltip: true, minWidth: 160 },
              { title: "复核人", t: "page.form.reviewer", key: 'reReviewBy', align: 'center', showOverflowTooltip: true, minWidth: 120 },
            ]
          },
          // {title: '出款', align: 'center', children: [
          //   {title: '出款说明', key: 'moneyRemark', align: 'center', showOverflowTooltip: true, minWidth: 200},
          //   {title: '出款时间', key: 'moneyTime', align: 'center', showOverflowTooltip: true, minWidth: 160},
          //   {title: '出款人', key: 'moneyBy', align: 'center', showOverflowTooltip: true, minWidth: 120},
          // ]},
          { title: "系统处理状态", t: "page.form.Systemprocessingstate", key: 'strStatus', align: 'center', minWidth: 120, showOverflowTooltip: true },
        ],
        list: [],
        pagination: {
          currentPage: 1,
          pageSize: 50,
          total: 0,
          pageSizes: [50, 100, 200]
        },
        current: null,
      },

      dateTimeRange: []
    }
  },
  mounted() {

    this.mainQueryRaw = Object.copy(this.mainQuery);

    var re = this.fetchOptions()
      .finally(() => {
        this.resetMainData();
      });

  },

  methods: {

    canCheck() {
      if (this.mainTable.loading) return false;

      if (!this.mainTable.current) return false;
      return this.mainTable.current.handleStatus == 0 || this.mainTable.current.handleStatus == 2 || this.mainTable.current.handleStatus == 4
    },

    canRecheck() {
      if (this.mainTable.loading) return false;
      if (!this.mainTable.current) return false;
      return this.mainTable.current.handleStatus == 1
    },

    canMoney() {
      if (this.mainTable.loading) return false;
      if (!this.mainTable.current) return false;
      return this.mainTable.current.canMoney;
    },

    canUnfreeze() {
      if (this.mainTable.loading) return false;
      if (!this.mainTable.current) return false;
      return this.mainTable.current.canUnfreeze;
    },

    handleSearch() {
      this.mainTable.pagination.currentPage = 1;
      this.fetchMainData();
    },

    renderMainData(re) {

      re.list.forEach(item => {
        item.floatAmount = item.amount / 100;

        // item.strAccountType = util.tool.strFromOptions(item.accountType, this.accountTypeOptions, '-');
        // item.strType = util.tool.strFromOptions(item.type, this.typeOptions, '-');
        // item.strStep = util.tool.strFromOptions(item.handleStatus, this.stepOptions, '-');
        // item.strStatus = util.tool.strFromOptions(item.status, this.statusOptions, '-');

      });

      this.mainTable.list = re.list;
      this.mainTable.pagination.total = re.total;
    },

    handleMainSizeChange(val) {
      this.mainTable.pagination.pageSize = val;
      this.mainTable.pagination.currentPage = 1;
      this.fetchMainData()
    },
    handleMainPageChange(val) {
      this.mainTable.pagination.page = val;
      this.fetchMainData()
    },

    onDateTimePickerChange() {
      this.mainQuery.startTime = this.dateTimeRange && this.dateTimeRange.length > 0 ? this.dateTimeRange[0] : ''
      this.mainQuery.endTime = this.dateTimeRange && this.dateTimeRange.length > 0 ? this.dateTimeRange[1] : ''

      this.fetchMainData();
    },

    buildMainQuery() {

      var query = {
        page: this.mainTable.pagination.currentPage,
        limit: this.mainTable.pagination.pageSize,
        search: this.mainQuery,
      };

      return query;
    },

    resetMainData() {

      this.mainQuery = Object.copy(this.mainQueryRaw);
      this.fetchMainData();
    },

    async fetchOptions() {

      var re = await ApiMain.getTypeOptions();
      this.typeOptions = re;

      var re = await ApiMain.getStepOptions();
      this.stepOptions = re;

      // var re = await ApiMain.getStatusOptions();
      // this.statusOptions = re;

      var re = await ApiMain.getAccountTypeOptions();
      this.accountTypeOptions = re;

    },

    async fetchMainData() {

      this.mainTable.loading = true;

      try {
        var re = await ApiMain.list(this.buildMainQuery());
        this.renderMainData(re);
      } catch (err) { }

      this.mainTable.loading = false;

    },

    onMainCurrentChange(now, old) {

      this.mainTable.current = now;
    },

    onMainDoubleClick(now) {
      this.mainTable.current = now;
      this.handleDetail();
    },

    handleAction(action) {

    },
    handleDetail() {
      if (!!this.mainTable.current) {
        this.$refs['dialog-detail'].show(Object.copy(this.mainTable.current));
      }
    },

    handleExportExcel() {
      this.$refs['common-export'].handleExportExcel(ApiMain, this.mainQuery);
    },
  }
}
</script>

<style>
</style>
